package com.example.javaow.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.javaow.domain.base.PageResult;
import com.example.javaow.domain.base.ResponseResult;
import com.example.javaow.domain.entity.DeviceMessage;
import com.example.javaow.service.DeviceMessageService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 设备消息控制器
 * 处理设备消息相关的 HTTP 请求
 */
@RestController
@RequestMapping("/device-message")
@RequiredArgsConstructor
@Slf4j
public class DeviceMessageController {

    @Autowired
    private DeviceMessageService deviceMessageService;

    /**
     * 获取所有设备消息列表
     *
     * @return 设备消息列表
     */
    @GetMapping("/getall")
    public ResponseResult<List<DeviceMessage>> getAllDeviceMessages() {
        log.info("查询所有设备消息");
        List<DeviceMessage> deviceMessages = deviceMessageService.list();
        return ResponseResult.success("查询成功", deviceMessages);
    }

    /**
     * 分页获取设备消息列表
     *
     * @param pageNum 页码
     * @param pageSize 每页大小
     * @return 分页结果
     */
    /**
     * 分页获取设备消息列表（带查询条件）
     *
     * @param pageNum 页码
     * @param pageSize 每页大小
     * @param messageType 消息类型
     * @param deviceAddress 设备地址
     * @param deviceName 设备名称
     * @param status 通讯状态
     * @return 分页结果
     */
    @GetMapping("/page")
    public ResponseResult<PageResult<DeviceMessage>> getDeviceMessagesPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String messageType,
            @RequestParam(required = false) String deviceAddress,
            @RequestParam(required = false) String deviceName,
            @RequestParam(required = false) String status) {
        log.info("分页查询设备消息，页码：{}，每页大小：{}，消息类型：{}，设备地址：{}，设备名称：{}，通讯状态：{}",
                pageNum, pageSize, messageType, deviceAddress, deviceName, status);

        IPage<DeviceMessage> pageResult = deviceMessageService.getPageWithConditions(
                pageNum, pageSize, messageType, deviceAddress, deviceName, status);

        PageResult<DeviceMessage> result = new PageResult<>();
        result.setTotal(pageResult.getTotal());
        result.setPages((int) pageResult.getPages());
        result.setCurrent((int) pageResult.getCurrent());
        result.setSize((int) pageResult.getSize());
        result.setRecords(pageResult.getRecords());

        return ResponseResult.success("查询成功", result);
    }


    /**
     * 根据 ID 获取设备消息详情
     *
     * @param id 设备消息 ID
     * @return 设备消息对象
     */
    @GetMapping("get/{id}")
    public ResponseResult<DeviceMessage> getDeviceMessageById(@PathVariable Long id) {
        log.info("根据ID查询设备消息：{}", id);
        DeviceMessage deviceMessage = deviceMessageService.getById(id);
        if (deviceMessage != null) {
            return ResponseResult.success("查询成功", deviceMessage);
        } else {
            return ResponseResult.error("设备消息不存在");
        }
    }

    /**
     * 创建新的设备消息
     *
     * @param deviceMessage 设备消息对象
     * @return 是否创建成功
     */
    @PostMapping("/add")
    public ResponseResult addDeviceMessage(@RequestBody DeviceMessage deviceMessage) {
        log.info("添加设备消息：{}", deviceMessage);
        boolean result = deviceMessageService.save(deviceMessage);
        if (result) {
            return ResponseResult.success("添加成功");
        } else {
            return ResponseResult.error("添加失败");
        }
    }

    /**
     * 更新设备消息
     *
     * @param deviceMessage 设备消息对象
     * @return 是否更新成功
     */
    @PostMapping("/update")
    public ResponseResult updateDeviceMessage(@RequestBody DeviceMessage deviceMessage) {
        log.info("更新设备消息：{}", deviceMessage);
        boolean result = deviceMessageService.updateById(deviceMessage);
        if (result) {
            return ResponseResult.success("更新成功");
        } else {
            return ResponseResult.error("更新失败");
        }
    }

    /**
     * 根据 ID 删除设备消息
     *
     * @param id 设备消息 ID
     * @return 是否删除成功
     */
    @DeleteMapping("/{id}")
    public ResponseResult deleteDeviceMessage(@PathVariable Long id) {
        log.info("删除设备消息：{}", id);
        boolean result = deviceMessageService.removeById(id);
        if (result) {
            return ResponseResult.success("删除成功");
        } else {
            return ResponseResult.error("删除失败");
        }
    }
}
